package _06_Time;

import java.util.Random;

public class _03_lianxi {

	public static void main(String[] args) {
		Thread t1 = new MyThread();
		Thread t2 = new MyThread();
		t1.setName("第一个线程:  ");
		t2.setName("第二个线程:  ");
		t1.start();
		t2.start();

	}

}

class MyThread extends Thread {
	// 两个线程添加3个数据,所以声明数组6个空间
	int a[] = new int[6];
	// 添加数组元素的下标
	int j = 0;

	@Override
	public void run() {
		for (int i = 0; i < 3; i++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			add();
		}
	}

	public void add() {
		// 随机数
		Random r = new Random();
		int num = r.nextInt(100) + 1;
		// 把这个随机数放进数组中
		a[j] = num;
		System.out.println(Thread.currentThread().getName() + "a[" + j + "]=" + num);
		j++;
	}
}
